package dao.impl;

import core.BaseDao;
import dao.OrderFoodDao;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Food;
import pojo.OrderFood;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderFoodDaoImpl extends BaseDao implements OrderFoodDao {

    /**
     * 通过orderId查找订单食品信息
     * @param orderId
     * @return
     */
    @Override
    public List<OrderFood> findOrderFoodByOrderId(String orderId) {
        try {
            sql = "SELECT * FROM order_food WHERE order_id=?";
            return queryRunner.query(sql, new BeanListHandler<>(OrderFood.class, rowProcessor), orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 减少订单中的某项食品
     * @param orderFoodId
     * @param foodId
     * @param foodCount
     * @return
     */
    @Override
    public Integer subOrderFood(String orderFoodId, String foodId, String foodCount) {
        try {
            if ("0".equals(foodCount)){
                sql = "DELETE  FROM order_food WHERE order_food_id=? AND food_id=?";
                return queryRunner.update(sql, orderFoodId, foodId);
            }
            sql = "UPDATE order_food SET food_count=? WHERE order_food_id=? AND food_id=?";
            queryRunner.update(sql, foodCount, orderFoodId, foodId);
            // 更新总价
            sql = "UPDATE order_food SET food_Money=food_count*food_price WHERE order_food_id=?";
            return queryRunner.update(sql, orderFoodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除
     * @param orderId
     * @param foodId
     * @return
     */
    @Override
    public Integer delete(String orderId, String foodId) {
        try {
            sql = "DELETE FROM order_food WHERE order_id=? AND food_id=?";
            return queryRunner.update(sql, orderId, foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加已经有的菜品
     * @param orderId
     * @param foodId
     * @param foodCount
     * @param foodMoney
     * @return
     */
    @Override
    public Integer addOrderFood(String orderId, String foodId, String foodCount, String foodMoney) {
        try {
            sql = "UPDATE order_food SET food_count=?,food_money=? WHERE food_id=? AND order_id=?";
            return queryRunner.update(sql, foodCount, foodMoney, foodId, orderId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 为餐桌添加新的菜品
     * @param orderId
     * @param food
     * @return
     */
    @Override
    public Integer addNewOrderFood(String orderId, Food food) {
        try {
            sql = "INSERT INTO order_food(order_id, food_id, food_count, food_price, food_money) VALUES (?,?,1,?,?)";
            return queryRunner.update(sql, orderId, food.getFoodId(), food.getFoodPrice(), food.getFoodPrice());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
